package club.terroir.client.produit;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import club.terroir.client.producteur.ProduitListGUI;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Folder   implements Serializable,IFolder{

	
	private static final long serialVersionUID = 1L;
	public  List<IFolder> childreen = new ArrayList<IFolder>();
	protected Folder parent;
	public String id;
	protected String name ;
	
	public Folder() {
		super();
	}

	public Folder(String s,Folder parent) {
		this();
		this.name = s;
		this.parent = parent;
	}
	private Label getWidgetSelection() {
		final Label label = new Label(this.name);
		return label;
	}

	private Label getWidgetModification() {
		final Label label = new Label(this.name);
		label.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				final PopupPanel popup = new PopupPanel(true, true);
				Panel vPanel = new VerticalPanel();
				final TextBox textField = new TextBox();
				Button buttonEnregistrer = new Button("Enregistrer");
				buttonEnregistrer.addClickHandler(new ClickHandler() {					
					@Override
					public void onClick(ClickEvent event) {
						String nameNew = textField.getText();
						name = nameNew;
						label.setText(nameNew);
						FolderFactory.instance.enregistrerFolder(Folder.this);
						popup.hide();
					}
				});
				Button buttonDelete= new Button("Supprimer");
				buttonDelete.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						if (parent != null){
							parent.childreen.remove(Folder.this);
							label.setVisible(false);
							popup.hide();
							FolderFactory.instance.enregistrerFolder(Folder.this.parent);
						}
					}
				});
				Button buttonCANCEL = new Button("Anuller");
				buttonCANCEL.addClickHandler(new ClickHandler() {					
					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
					}
				});
				Button buttonAddFolder = new Button("Ajouter un dossier");
				buttonAddFolder.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
						ajouterDossierDisplayPopup();
						FolderFactory.instance.enregistrerFolder(Folder.this.parent);
					}
				});
				Button buttonAddProduit = new Button("Ajouter un produit");
				buttonAddProduit.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						popup.hide();
						ajouterProduitDisplayPopup();
						FolderFactory.instance.enregistrerFolder(Folder.this);
					}
				});
				textField.setText(name);
				vPanel.add(textField);
				vPanel.add(buttonEnregistrer);
				vPanel.add(buttonDelete);
				vPanel.add(buttonAddFolder);
				vPanel.add(buttonAddProduit);
				vPanel.add(buttonCANCEL);
				popup.add(vPanel);
				popup.setSize("200", "200");
				popup.center();
				popup.show();
			}
		});
		return label;
	}

	@Override
	public TreeItem createTreeItemModification() {
		
		TreeItem treeItem = new TreeItem(getWidgetModification());
		for(IFolder f :childreen){
			treeItem.addItem(f.createTreeItemModification());
		}
		return treeItem;
	}
	
	@Override
	public TreeItem createTreeItemSelection() {
		
		TreeItem treeItem = new TreeItem(getWidgetSelection());
		for(IFolder f :childreen){
			treeItem.addItem(f.createTreeItemSelection());
		}
		return treeItem;
	}
	
	void ajouterDossierDisplayPopup(){
		final PopupPanel popupAjoutDossier = new PopupPanel(true, true);
		Panel vPanel = new VerticalPanel();
		final TextBox textField = new TextBox();
		
		
		Button buttonCANCEL = new Button("Anuller");
		buttonCANCEL.addClickHandler(new ClickHandler() {					
			@Override
			public void onClick(ClickEvent event) {
				popupAjoutDossier.hide();
			}
		});
		Button buttonAddOk = new Button("OK ");
		buttonAddOk.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				String nameNewDossier = textField.getText();
				
				Folder fNew = new Folder(nameNewDossier, Folder.this);
				FolderFactory.instance.enregistrerFolder(fNew);
				childreen.add(fNew);
				ProduitListGUI.instance.updateUI();
				popupAjoutDossier.hide();
			}
		});
		
		
		vPanel.add(new Label("Ajouter un dossier :"));
		vPanel.add(textField);
		vPanel.add(buttonAddOk);
		vPanel.add(buttonCANCEL);
		popupAjoutDossier.add(vPanel);
		popupAjoutDossier.setSize("200", "200");
		popupAjoutDossier.center();
		popupAjoutDossier.show();
	}
	
	
	void ajouterProduitDisplayPopup(){
		final PopupPanel popupAjoutProduit = new PopupPanel(true, true);
		Panel vPanel = new VerticalPanel();
		final TextBox textField = new TextBox();
		
		
		Button buttonCANCEL = new Button("Anuller");
		buttonCANCEL.addClickHandler(new ClickHandler() {					
			@Override
			public void onClick(ClickEvent event) {
				popupAjoutProduit.hide();
			}
		});
		Button buttonAddOk = new Button("OK ");
		buttonAddOk.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				String nameNewProduit = textField.getText();
				childreen.add(new Produit(nameNewProduit, Folder.this));
				ProduitListGUI.instance.updateUI();
				popupAjoutProduit.hide();
			}
		});
		
		
		vPanel.add(new Label("Ajouter un produit :"));
		vPanel.add(textField);
		vPanel.add(buttonAddOk);
		vPanel.add(buttonCANCEL);
		popupAjoutProduit.add(vPanel);
		popupAjoutProduit.setSize("200", "200");
		popupAjoutProduit.center();
		popupAjoutProduit.show();	}

	/**
	 * @return the childreen
	 */
	public List<IFolder> getChildreen() {
		return childreen;
	}

	/**
	 * @return the parent
	 */
	public Folder getParent() {
		return parent;
	}

	



	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param childreen the childreen to set
	 */
	public void setChildreen(List<IFolder> childreen) {
		this.childreen = childreen;
	}

	/**
	 * @param parent the parent to set
	 */
	public void setParent(Folder parent) {
		this.parent = parent;
	}

	

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	
	
}
